package cn.stylefeng.guns.modular.api.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.api.entity.Plan;
import cn.stylefeng.guns.modular.api.model.enums.PlanStatusEnum;
import cn.stylefeng.guns.modular.api.model.params.PlanParam;
import cn.stylefeng.guns.modular.api.model.result.PlanResult;
import cn.stylefeng.guns.modular.api.service.PlanService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.util.ValidateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Api;

import java.awt.*;


/**
 * 生产计划控制器
 *
 * @author wusonghe
 * @Date 2019-08-31 00:07:13
 */
@Controller
@Api(tags="生产计划")
@RequestMapping("/api/plan")
public class PlanController extends BaseController {

    private String PREFIX = "/plan";

    @Autowired
    private PlanService planService;

    /**
     * 跳转到主页面
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/plan.html";
    }

    /**
     * 新增页面
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/plan_add.html";
    }

    /**
     * 编辑页面
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/plan_edit.html";
    }

    /**
     * 新增接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @Transactional
	@PostMapping("/addItem")
    @ResponseBody
//    @ApiOperation(value = "新增接口", notes = "新增", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "新增", response = ResponseData.class)})
    public ResponseData addItem(@RequestBody PlanParam planParam) throws Exception {


        planParam.setStatus(PlanStatusEnum.GROW.getCode());
        //比較時間
        this.planService.add(planParam);
        return ResponseData.success();
    }

    /**
     * 更新接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @Transactional
	@PostMapping("/editItem")
    @ResponseBody
//    @ApiOperation(value = "更新接口", notes = "更新", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "更新", response = ResponseData.class)})
    public ResponseData editItem(@RequestBody PlanParam planParam) {


        if (planParam.getOnlySave() == 0) {
            if (planParam.getStatus().equals(PlanStatusEnum.NORAML.getCode())) {
                planParam.setStatus(PlanStatusEnum.GROW.getCode());
            }else if (planParam.getStatus().equals(PlanStatusEnum.GROW.getCode())) {
                planParam.setStatus(PlanStatusEnum.HARVEST.getCode());
            }else if (planParam.getStatus().equals(PlanStatusEnum.HARVEST.getCode())) {
                planParam.setStatus(PlanStatusEnum.DONE.getCode());
            }
        }


        this.planService.update(planParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping("/delete")
    @ResponseBody
//    @ApiOperation(value = "删除接口", notes = "删除", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "详情", response = ResponseData.class)})
    public ResponseData delete(PlanParam planParam) {
        this.planService.delete(planParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping("/detail")
    @ResponseBody
    @ApiOperation(value = "详情接口", notes = "详情", response = PlanResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "详情", response = PlanResult.class)})
    public ResponseData detail(PlanParam planParam) {
        Plan detail = this.planService.getById(planParam.getPlanId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping(value = "/list")
    @ResponseBody
    @ApiOperation(value = "列表接口", notes = "列表", response = PlanResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "列表", response = PlanResult.class)})
    public LayuiPageInfo list(PlanParam planParam) {

        //拼接查询条件
        String beginTime = "";
        String endTime = "";

        if (ToolUtil.isNotEmpty(planParam.getTimeLimit())) {
            String[] split = planParam.getTimeLimit().split(" - ");
            beginTime = split[0];
            endTime = split[1];
        }
        planParam.setBeginTime(beginTime);
        planParam.setEndTime(endTime);

        return this.planService.findPageBySpec(planParam);
    }

}


